<?php
	defined( "AZ_EXE_" ) or die ( "ERROR : CORE VARIABLE IS NOT DEFINED." );
	Import::html();
	Import::util( "XMLArray" );
	class View extends AZ {
		
		private $vars = array();
		private $viewType = array();
		private $layout = null ;
		private $miniLayout = null ;
		private $element = array() ;
		private $html = null ;
		private $lang = array();
		private $css = "" ;
		private $js = "" ;
		
		public function setCss($cssName=""){
			if ( Basic::fileExist( CSS_FOLDER . $cssName . ".css" ) ){
				$this->css=$cssName;
			}
		}
		public function setJs($jsName=""){
			if ( Basic::fileExist( JS_FOLDER . $jsName . ".js" ) ){
				$this->js=$jsName;
			}
		}
		public function __construct( $viewType = array() , $viewVars = array() , $language = "" ){
			$this->viewType = $viewType ;
			$this->vars = $viewVars ;
			$this->html = new HTML();
			$this->loadLanguage($language);
		}
		public function loadLanguage( $language ){
			$xml2array = new XMLArray();
			$this->lang = $xml2array->getLanguage( $language );
			$xml2array = null ;
		}
		public function View( $viewType = array() , $viewVars = array() , $language  ){
			$this->__construct( $viewType , $viewVars , $language );
		}
		public function render( $controllerName = "" ){
			foreach ( $this->viewType AS $key => $value ){
				if ( strtolower($key) == "layout" ){
					$this->layout = $value ;
					$this->renderLayout();
					break;
				}
				else if ( strtolower($key) == "element" ){
					$this->element[] = $value ;
					$this->renderElement();
					break;
				}
				else if ( strtolower($key) == "default" ){
					$this->miniLayout = $value ;
					$this->renderDefault( $controllerName );
					break;
				}
			}
		}
		public function element( $elementName , $return = false ){
			$this->element[] = $elementName ;
			if ( Basic::fileExist( APP_FOLDER . "views" . DS . "element" . DS . $elementName . ".az" ) ){
				extract( $this->vars , EXTR_SKIP);
				//extract( $this->lang , EXTR_SKIP);
				ob_start();
				// get element of core library 
				//include( TEMPLATE_FOLDER . "elements" . DS . $elementName . ".az" );
				include( APP_FOLDER . "views" . DS . "element" . DS . $elementName . ".az" );
				if ( $return )return ob_get_clean();
				else echo ob_get_clean();
			}
			else {
				die ( "=>ERROR : Element with this name $elementName not found." );
			}
		}
		private function renderLayout(){
			if ( $this->layout == null || $this->layout == "" ){
				if ( Basic::fileExist( CORE_LAYOUT_FOLDER . $this->layout . ".az" ) ){
					extract( $this->vars , EXTR_SKIP);
					//extract( $this->lang , EXTR_SKIP);
					ob_start();
					include( CORE_LAYOUT_FOLDER . $this->layout . ".az" );
					echo ob_get_clean();
				}
				else {
					die ( "ERROR : Layout with this name $this->layout not found." );
				}
			}
			else{
				if ( Basic::fileExist( APP_FOLDER . "views" . DS . "layout" . DS . $this->layout . ".az" ) ){
					extract( $this->vars , EXTR_SKIP);
					//extract( $this->lang , EXTR_SKIP);
					ob_start();
					include( APP_FOLDER . "views" . DS . "layout" . DS . $this->layout . ".az" );
					echo ob_get_clean();
				}
				else {
					die ( "ERROR : Layout with this name $this->layout not found." );
				}
			}
		}
		private function renderDefault( $controllerName){
			if ( Basic::fileExist( APP_FOLDER . "views" . DS . $controllerName . DS . $this->miniLayout . ".az" ) ){
				extract( $this->vars , EXTR_SKIP);
				//extract( $this->lang , EXTR_SKIP);
				ob_start();
				include( APP_FOLDER . "views" . DS . $controllerName . DS . $this->miniLayout . ".az" );
				echo ob_get_clean();
			}
			else {
				echo APP_FOLDER . "views" . DS . $controllerName . DS . $this->miniLayout . ".az<br/>" ;
				die ( "ERROR : Layout with this name $this->miniLayout not found." );
			}
		}
		private function renderElement(){
			$element = $this->element[0];
			if ( Basic::fileExist( APP_FOLDER . "views" . DS . "element" . DS . $element . ".az" ) ){
				extract( $this->vars , EXTR_SKIP);
				//extract( $this->lang , EXTR_SKIP);
				ob_start();
				include( APP_FOLDER . "views" . DS . "element" . DS . $element . ".az" );
				echo ob_get_clean();
			}
			else {
				die ( "ERROR : Element with this name $element not found." );
			}
		}
		public function getLang($langKeyName=""){
			$this->lang[$langKeyName];
		}
	}
?>